using Finance.Api.Read.Applications.Commands.库存调拨;
using Finance.Api.Read.Applications.Commands.调拨出入库记录;
using Finance.ErrorCode;
using MediatR;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace Finance.Api.Read.Controllers.库存调拨
{
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DeploymentController : ControllerBase
    {
        private readonly IMediator mediator;
        private readonly ILogger<DeploymentController> logger;

        public DeploymentController(IMediator mediator, ILogger<DeploymentController> logger)
        {
            this.mediator = mediator;
            this.logger = logger;
        }

        /// <summary>
        /// 库存调拨分页查询
        /// </summary>
        /// <param name="index">页码</param>
        /// <param name="size">每页大小</param>
        /// <param name="Deployment">调拨单号</param>
        /// <param name="DeploymentStyle">调拨类型</param>
        /// <param name="DeploymentInto">调入仓库</param>
        /// <param name="DeploymentOut">调出仓库</param>
        /// <param name="StartTime">开始时间</param>
        /// <param name="EndTime">结束时间</param>
        /// <param name="CreateName">创建人员</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<ApiPaging<List<Dtos.DeploymentDto>>>> QueryDeployment(
            int index = 1, 
            int size = 10,
            string Deployment = null,
            string DeploymentStyle = null,
            string DeploymentInto = null,
            string DeploymentOut = null,
            DateTime? StartTime = null,
            DateTime? EndTime = null,
            string CreateName = null)
        {
            try
            {
                logger.LogInformation("库存调拨分页查询");
                var request = new DeploymentQueryCommand
                {
                    index = index,
                    size = size,
                    Deployment = Deployment,
                    DeploymentStyle = DeploymentStyle,
                    DeploymentInto = DeploymentInto,
                    DeploymentOut = DeploymentOut,
                    StartTime = StartTime,
                    EndTime = EndTime,
                    CreateName = CreateName
                };
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("异常信息=》" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取调拨明细
        /// </summary>
        /// <param name="deploymentId">调拨单ID</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Domain.调拨.DeploymentDetail>>> GetDeploymentDetails(int deploymentId)
        {
            try
            {
                logger.LogInformation("获取调拨明细，调拨单ID: {DeploymentId}", deploymentId.ToString());
                var request = new DeploymentDetailQueryCommand
                {
                    DeploymentId = deploymentId
                };
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("异常信息=》" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取调拨入库记录
        /// </summary>
        /// <param name="deployment">调拨单号</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Dtos.DeployGoinRecordDto>>> GetDeployGoinRecords(string deployment)
        {
            try
            {
                logger.LogInformation("获取调拨入库记录，调拨单号: {Deployment}", deployment);
                var request = new DeployGoinRecordQueryCommand
                {
                    Deployment = deployment
                };
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("异常信息=》" + ex.Message);
                throw;
            }
        }

        /// <summary>
        /// 获取调拨出库记录
        /// </summary>
        /// <param name="deployment">调拨单号</param>
        /// <returns></returns>
        [HttpGet]
        public async Task<ApiResult<List<Dtos.DeployOutRecordDto>>> GetDeployOutRecords(string deployment)
        {
            try
            {
                logger.LogInformation("获取调拨出库记录，调拨单号: {Deployment}", deployment);
                var request = new DeployOutRecordQueryCommand
                {
                    Deployment = deployment
                };
                return await mediator.Send(request);
            }
            catch (Exception ex)
            {
                logger.LogError("异常信息=》" + ex.Message);
                throw;
            }
        }
    }
} 